package ModelLayer;
import java.util.*;
/**
 * Write a description of class AddressBook here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */
public class AddressBook
{

    private HashSet<Person> addressBook;
    private static AddressBook _instance = null;
    

    
    
    public static AddressBook getInstance()
    {
        if(null == _instance) 
        {
            _instance = new AddressBook();
        }
        return _instance;
    }
    
    public void readLoanName(String name)
    {
        Iterator<Person> it=addressBook.iterator();
        while(it.hasNext())
        {
            Person person=it.next();
            if(person.readName().equals(name))
            {
                Iterator<Loan> it2=person.readLoans().iterator();
                while(it2.hasNext())
                {
                    Loan loan=it2.next();
                    System.out.println("***************");
                    System.out.println("ID: "+loan.readID());
                    System.out.println("BorrowDate: "+loan.readBorrowDate());
                    System.out.println("State: "+loan.readState());
                }
            }
        }
    }
 
    
    public void createLoan(Copy copy,String name, int id, String borrowDate, String period)
    {
        Iterator<Person> it = addressBook.iterator();
        while(it.hasNext()) 
        {
            Person person = it.next();
            if(person.readName().equals(name))
                {
                    person.createLoan(copy,id,borrowDate,period);
                    
                }
        }
    }
    
    public Person checkPerson(String name)
    {
        Iterator<Person> it = addressBook.iterator();
        while(it.hasNext()) 
        {
            Person person = it.next();
            if(person.readName().equals(name))
                return person;
        }
        return null;
    }
    
    public AddressBook()
    {
        addressBook=new HashSet<>();
    }
    
    public boolean checkID(int id)
    {
         Iterator<Person> it = addressBook.iterator();
        while(it.hasNext()) 
        {
            Person person = it.next();
            if(id==person.readID())
                return true;
        }
        return false;
    }
    
    public int readSize()
    {
       return addressBook.size();
    }
    
     public void printPersons()
    {
        int count=1;
        Iterator<Person> it = addressBook.iterator();
        while(it.hasNext()) 
        {
            Person person = it.next();
            System.out.println(count+":");
            System.out.println("ID: "+person.readID());
            System.out.println("Name: "+person.readName());
            count++;
        }
    }
    
    public Boolean addPerson(int id, String name, String address, int postalCode, String city, String phone)
    {   
        Iterator<Person> it = addressBook.iterator();
        while(it.hasNext()) 
        {
            Person person = it.next();
             if(id==person.readID())
            {
                return false;
            }
        }
        Person newPerson=new Person();
        newPerson.create(id, name, address, postalCode, city, phone);
        addressBook.add(newPerson); 

        return true;
    }
    
    public Boolean removePerson(int id)
    {
        Iterator<Person> it = addressBook.iterator();
        while(it.hasNext()) 
        {
            Person person = it.next();
            if(id==person.readID()) 
            {
                addressBook.remove(person);
                System.out.println("DELETED!");
                return true;
            }
        }
        return false;
        
    }
    
    public void updatePersonID(int id,int newID)
    {
        Person person=returnPerson(id);
        person.updateID(newID);
    }
    
    public void updatePersonName(int id,String name)
    {
        Person person=returnPerson(id);
        person.updateName(name);
    }
    
    public void updatePersonAddress(int id,String address)
    {
        Person person=returnPerson(id);
        person.updateAddress(address);
    }
    
    public void updatePersonPostalCode(int id,int postalCode)
    {
        Person person=returnPerson(id);
        person.updatePostalCode(postalCode);
    }
    
    public void updatePersonCity(int id,String city)
    {
        Person person=returnPerson(id);
        person.updateCity(city);
    }
    
    public void updatePersonPhone(int id,String phone)
    {
        Person person=returnPerson(id);
        person.updatePhone(phone);
    }

    public int readArrayListSize()
    {
        return addressBook.size();
    }
    
    public Person returnPerson(int id)
    {
        Iterator<Person> it = addressBook.iterator();
        while(it.hasNext()) 
        {
            Person person = it.next();
            if(id==person.readID())
                return person;
        }
        return null;
    }
    
    public void printPerson(int id)
    {
        for(Person person: addressBook)
            if(id==person.readID())
                {
                    System.out.println("ID: "+person.readID());
                    System.out.println("Name: "+person.readName());
                    System.out.println("Address: "+person.readAddress());
                    System.out.println("Postal Code: "+person.readPostalCode());
                    System.out.println("City: "+person.readCity());
                    System.out.println("Phone: "+person.readPhone());
                    System.out.println("Number of loans: "+person.readLoans().size());
                }
    }
    
}
